package com.shop.cereshop.app.service.notice.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.dl.CereShopProductMapper;
import com.shop.cereshop.app.dao.notice.CereBuyerNoticeDAO;
import com.shop.cereshop.app.dao.notice.CereNoticeDAO;
import com.shop.cereshop.app.dao.order.CereShopOrderDAO;
import com.shop.cereshop.app.domain.TicketInfo.CereShopTicketInfo;
import com.shop.cereshop.app.page.notice.BuyerNotice;
import com.shop.cereshop.app.page.notice.NoticeDetail;
import com.shop.cereshop.app.page.order.OrderDetail;
import com.shop.cereshop.app.param.notice.NoticeIdParam;
import com.shop.cereshop.app.service.notice.CereNoticeService;
import com.shop.cereshop.app.service.order.DlCereOrderProductService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.domain.notice.CereBuyerNotice;
import com.shop.cereshop.commons.domain.notice.CereNotice;
import com.shop.cereshop.commons.domain.order.CereOrderProduct;
import com.shop.cereshop.commons.domain.product.CereShopProduct;
import com.shop.cereshop.commons.exception.CoBusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.PipedReader;
import java.util.ArrayList;
import java.util.List;

@Service
public class CereNoticeServiceImpl implements CereNoticeService {

    @Autowired
    private CereNoticeDAO cereNoticeDAO;

    @Autowired
    private CereBuyerNoticeDAO cereBuyerNoticeDAO;
    @Autowired
    private CereShopProductMapper cereShopProductMapper;
    @Autowired
    private CereShopOrderDAO cereShopOrderDAO;
    @Autowired
    private DlCereOrderProductService dlCereOrderProductService;

    @Override
    public Page getAll(PageParam pageParam, Long buyerUserId) throws CoBusinessException {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<BuyerNotice> list = cereNoticeDAO.getAll(buyerUserId);
        List<BuyerNotice> list2 = new ArrayList<>();
        for (BuyerNotice c : list){
            Long orderId = c.getOnly();
            OrderDetail detail=cereShopOrderDAO.getById(orderId);
            //YF:根据orderId查询出附属表数据并赋值返回到前端
            CereShopTicketInfo cereShopTicketInfo = dlCereOrderProductService.findCereShopTicketInfoByOrderId(orderId);
            if (cereShopTicketInfo != null) {
                detail.setTicketInfoId(cereShopTicketInfo.getTicketInfoId());
                detail.setCustomer(cereShopTicketInfo.getCustomer());
                detail.setCustomerTel(cereShopTicketInfo.getCustomerTel());
                detail.setCustomerCard(cereShopTicketInfo.getCustomerCard());
                detail.setMemo(cereShopTicketInfo.getMemo());
                detail.setTravelDate(cereShopTicketInfo.getTravelDate());
                //YF:根据orderId查询出产品信息，并赋值productNo产品编号
//                CereOrderProduct product = dlCereOrderProductService.findCereOrderProductByOrderId(orderId);
                List<CereOrderProduct> cereOrderProductByOrderId = dlCereOrderProductService.findCereOrderProductByOrderId(orderId);
                CereOrderProduct product = cereOrderProductByOrderId.get(0);
                        CereShopProduct cereShopProduct = dlCereOrderProductService.findCereShopProductByProductId(product.getProductId());
                if (cereShopProduct!=null){
                    detail.setProductNo(cereShopProduct.getProductNo());
                    c.setShopProduct(cereShopProduct);
                }
            }
            list2.add(c);
        }
        PageInfo<BuyerNotice> pageInfo = new PageInfo<>(list2);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public NoticeDetail getById(NoticeIdParam param, CereBuyerUser user) throws CoBusinessException {
        //新增关系数据
        CereBuyerNotice cereBuyerNotice = new CereBuyerNotice();
        cereBuyerNotice.setNoticeId(param.getNoticeId());
        cereBuyerNotice.setBuyerUserId(user.getBuyerUserId());
        cereBuyerNoticeDAO.insert(cereBuyerNotice);
        return cereNoticeDAO.getById(param.getNoticeId());
    }

    @Override
    public List<BuyerNotice> getGongGaoAll(Long buyerUserId) throws CoBusinessException {
        return cereNoticeDAO.getGongGaoAll(buyerUserId);
    }

    @Override
    public void insert(CereNotice cereNotice) throws CoBusinessException {
        cereNoticeDAO.insert(cereNotice);
    }

    @Override
    public void insertBatch(List<CereNotice> notices) throws CoBusinessException {
        cereNoticeDAO.insertBatch(notices);
    }

    @Override
    public void updateBuyerData(Long buyerUserId, Long id) throws CoBusinessException {
        cereNoticeDAO.updateBuyerData(buyerUserId, id);
    }

    @Override
    public int getNotRead(Long buyerUserId) {
        return cereNoticeDAO.getNotRead(buyerUserId);
    }

    @Override
    public void update(CereNotice cereNotice) throws CoBusinessException {
        cereNoticeDAO.updateByPrimaryKeySelective(cereNotice);
    }

    @Override
    public int getRead(Long buyerUserId) {
        return cereNoticeDAO.getRead(buyerUserId);
    }

    @Override
    public List<Long> findIdsByOrderId(Long orderId) {
        return cereNoticeDAO.findIdsByOrderId(orderId);
    }

    @Override
    public void deleteByIds(List<Long> noticeIds) throws CoBusinessException {
        cereNoticeDAO.deleteByIds(noticeIds);
    }

    @Override
    public void deleteBuyerNotice(List<Long> noticeIds, Long buyerUserId) throws CoBusinessException {
        cereNoticeDAO.deleteBuyerNotice(noticeIds, buyerUserId);
    }
}
